package cn.cutie.clot.gateway.web.filter;

import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * @Description:
 * @Author: Cutie
 * @CreateDate: 2024/6/4 20:26
 * @Version: 0.0.1
 */
@Component
public class GatewayWebFilter implements WebFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        System.out.println("===>>> Clot Gateway web filter ...");
        if(exchange.getRequest().getQueryParams().getFirst("mock")==null) {
            return chain.filter(exchange);
        }

        // 如果 mock 有值，返回下面的内容
        String mock = """
                {"result": "mock"}
                """;
        return exchange.getResponse()
                .writeWith(Mono.just(exchange.getResponse()
                        .bufferFactory().wrap(mock.getBytes())));
    }
}
